<template>
  <my-window :title="title" :modal="modal" :wrapper-closable="true" :modal-append-to-body="modalAppendToBody"
    :visible.sync="visible" destroy-on-close size="100%" width="95%"
    drawer-body-style="padding:0px;height: calc(100% - 61px);" :before-close="onCancel" @opened="onSearch">
  </my-window>
</template>

<script>
  import {
    formatTime
  } from '@/utils'
  import {
    getListPage
  } from '@/api/base-data/bom'
  import MyContainer from '@/components/my-container'
  import MyWindow from '@/components/my-window'
  import MySearch from '@/components/my-search'

  export default {
    name: 'MySelectBom',
    components: {
      MyContainer,
      MyWindow,
      MySearch
    },
    props: {
      title: {
        type: String,
        default: '选择BOM'
      },
      visible: {
        type: Boolean,
        default: false
      },
      form: {
        type: String,
        default: ''
      },
      multiple: {
        type: Boolean,
        default: false
      },
      modal: {
        type: Boolean,
        default: false
      },
      modalAppendToBody: {
        type: Boolean,
        default: false
      },
      // 过滤条件
      filterGroups: {
        type: Object,
        default: null
      },
    },
    data() {
      return {
        filters: {
          product: '',
          sub: '',
        },
        dynamicFilter: {
          Groups: []
        },

        data: [],
        total: 0,
        listLoading: false,
        pageLoading: false,
        currentRow: null,
        sels: []
      }
    },
    methods: {
      // 取消
      onCancel() {
        this.$emit('update:visible', false)
        this.$emit('cancel')
      },
      // 确定
      onSure() {
        this.$emit('click', this.form, this.multiple ? this.sels : this.currentRow)
      },
      onSearch() {
        this.dynamicFilter.Groups = [];
        if (this.filterGroups) {
          this.dynamicFilter.Groups.push(this.filterGroups)
        }
        if (this.filters.product) {
          this.dynamicFilter.Groups.push({
            Type: 'OR',
            Groups: [{
              Condition: {
                field: 'Product.Code',
                operator: 'Contain',
                value: this.filters.product,
              }
            }, {
              Condition: {
                field: 'Product.Name',
                operator: 'Contain',
                value: this.filters.product,
              },
            }],
          });
        }
        if (this.filters.sub) {
          this.dynamicFilter.Groups.push({
            Type: 'OR',
            Groups: [{
              Condition: {
                field: 'Sub.Code',
                operator: 'Contain',
                value: this.filters.sub,
              }
            }, {
              Condition: {
                field: 'Sub.Name',
                operator: 'Contain',
                value: this.filters.sub,
              },
            }],
          });
        }
        this.$refs.pager.setPage(1)
        this.getData()
      },
      onCurrentChange(row) {
        this.currentRow = row
      },
      onSelectionChange(sels) {
        this.sels = sels
      },
      formatCreatedTime: function(row, column, time) {
        return formatTime(time, 'YYYY-MM-DD HH:mm')
      },
      // 获取数据
      async getData() {
        var pager = this.$refs.pager.getPager()
        const params = {
          ...pager,
          dynamicFilter: this.dynamicFilter
        }
        this.listLoading = true
        const res = await getListPage(params)
        this.listLoading = false

        if (!res?.success) {
          return
        }

        this.total = res.data.total
        this.data = res.data.list
      }
    }
  }
</script>
